package com.education.wisdom.service.impl;


import com.education.common.utils.Convert;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.mapper.MsPaperMapper;
import com.education.wisdom.mapper.MsPaperQuestionMapper;
import com.education.wisdom.mapper.MsQuestionsMapper;
import com.education.wisdom.mapper.MscatalogPaperMapper;
import com.education.wisdom.pojo.*;
import com.education.wisdom.service.IMsPaperService;
import com.education.wisdom.vo.MsPaperVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 试卷 服务层实现
 * 
 * @author ruoyi
 * @date 2018-12-17
 */
@Service
public class MsPaperServiceImpl implements IMsPaperService
{
	@Autowired
	private com.education.wisdom.mapper.MsPaperMapper MsPaperMapper;
	@Autowired
	private MscatalogPaperMapper catalogPaperMapper;
	@Autowired
	private MsPaperQuestionMapper paperQuestionMapper;
	@Autowired
	private MsQuestionsMapper questionMapper;

	/**
     * 查询试卷信息
     * 
     * @param id 试卷ID
     * @return 试卷信息
     */
    @Override
	public MsPaper selectMsPaperById(Integer id)
	{
	    return MsPaperMapper.selectMsPaperById(id);
	}


	/**
	 * 查询试卷
	 * @param sjCreateuser 创建人
	 * @return
	 */
	@Override
	public List<MsPaper> selectPaperByTeacher(Integer sjCreateuser) {
		return MsPaperMapper.selectPaperByTeacher(sjCreateuser);
	}

	/**
	 * 查询试卷
	 * @param sjCreateuser 创建人
	 * @return
	 */
	@Override
	public List<MsPaper> selectPaperBySubject(Integer sjCreateuser) {
		return MsPaperMapper.selectPaperBySubject(sjCreateuser);
	}

	/**
	 * 查询试卷
	 * @return
	 */
	@Override
	public List<MsPaper> selectPaperByTId(String userId) {
		return MsPaperMapper.selectPaperByTId(userId);
	}

	/**
     * 查询试卷列表
     * 
     * @param MsPaper 试卷信息
     * @return 试卷集合
     */
	@Override
	public List<MsPaper> selectMsPaperList(MsPaper MsPaper)
	{
	    return MsPaperMapper.selectMsPaperList(MsPaper);
	}

	@Override
	public List<MsPaper> selectStudentMsPaperList(MsPaper MsPaper) {
		return MsPaperMapper.selectStudentMsPaperList(MsPaper);
	}

	/**
     * 新增试卷
     *
     * @return 结果
     */
	@Override
	public void insertMsPaper(MsPaperVo paper)
	{
	/*	Integer[] questionIds=paper.getQuestionIds();
		BigDecimal scoure=new BigDecimal(0);
		if(questionIds.length>0){
			for(Integer questionId:questionIds){
				MsQuestions questions=questionMapper.selectMsQuestionsById(questionId);
				scoure=scoure.add(questions.getStScore());
			}
		}
*/
		paper.setSjId(UUIDUtils.getByUUId());
		paper.setCreateTime(new Date());
		//paper.setSjScore(scoure);
		MsPaperMapper.insertMsPaper(paper);
		/*MsCatalogPaper catalogPaper=new MsCatalogPaper();
		catalogPaper.setCatalogId(paper.getCatalogId());
		catalogPaper.setPaperId(paper.getId());
		insertPaperQuestion(paper);*/
//	    catalogPaperMapper.insertCatalogPaper(catalogPaper);
	}
	
	/**
     * 修改试卷
     *
     * @return 结果
     */
	@Override
	public int updateMsPaper(MsPaperVo paper){
		Integer[] questionIds=paper.getQuestionIds();
		Integer paperId=paper.getId();
		BigDecimal scoure=new BigDecimal(0);
		paperQuestionMapper.deleteByPaperId(paperId);
		if(questionIds.length>0){
			for(Integer questionId:questionIds){
				MsQuestions questions=questionMapper.selectMsQuestionsById(questionId);
				scoure=scoure.add(questions.getStScore());
			}
		insertPaperQuestion(paper);
		}
		paper.setSjScore(scoure);
		MsCatalogPaper catalogPaper=new MsCatalogPaper();
		catalogPaper.setCatalogId(paper.getCatalogId());
		catalogPaper.setPaperId(paper.getId());
//		catalogPaperMapper.updateCatalogPaper(catalogPaper);
		return MsPaperMapper.updateMsPaper(paper);
	}

	/**
     * 删除试卷对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteMsPaperByIds(String ids)
	{
		return MsPaperMapper.deleteMsPaperByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<MsPaper> selectPaperByClass(Map<String, Object> map) {
		return MsPaperMapper.selectPaperByClass(map);
	}

	@Override
	public List<MsUserPaper> selectMsPapersList(MsPaper MsPaper) {
		return MsPaperMapper.selectMsPapersList(MsPaper);
	}

	/**
	 * 新增试卷与试题关联信息
	 *
	 * @param paper 用户对象
	 */
	public void insertPaperQuestion(MsPaperVo paper)
	{
		List<MsPaperVo> list = new ArrayList<MsPaperVo>();
		for (Integer questionId : paper.getQuestionIds())
		{
			MsPaperQuestion paperQuestion = new MsPaperQuestion();
			paperQuestion.setPaperId(paper.getId());
			paperQuestion.setQuestionId(questionId);
			paperQuestionMapper.insertPaperQuestion(paperQuestion);
		}

	}
}
